package com.mengtian_tech.tju.dataquery.api.task.executors;

import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;

import com.mengtian_tech.tju.dataquery.api.model.tjuOracle.College;
import com.mengtian_tech.tju.dataquery.api.service.EmployeeService;
import com.mengtian_tech.tju.dataquery.api.service.PersonVisitLogService;
import com.mengtian_tech.tju.dataquery.api.service.StudentService;
import com.mengtian_tech.tju.dataquery.api.util.SpringUtil;

/**
 * 处理学院Id为空的行人进出记录
 */
public class HandleCollegeIdForPersonExecutor implements Runnable{

	
	
	@Override
	public void run() {
		ApplicationContext ctx = 	SpringUtil.getContext();
		PersonVisitLogService service = ctx.getBean(PersonVisitLogService.class);
		EmployeeService employeeService = ctx.getBean(EmployeeService.class);
		StudentService studentService = ctx.getBean(StudentService.class);
		
		List<Map<String,Object>> list = service.getNextHandleCollegeList();
	
		while(list.size() > 0) {
			doHandle(service, employeeService, studentService, list);
			
			list = service.getNextHandleCollegeList();
		}
		
		
	}

	private void doHandle(PersonVisitLogService service, EmployeeService employeeService, StudentService studentService,
			List<Map<String, Object>> list) {
		for(Map<String,Object> item:list) {
			Long id = new Long(item.get("id").toString());
			String userId = (String) item.get("job_no");
			
			String collegeId = "-1";
			String collegeName = null;
			
			if(userId != null) {
				College college = studentService.getCollege(userId);

				if(college == null) {
					college = employeeService.getCollege(userId);
				}
					
				
				if(college != null) {
					collegeId = college.getCollegeId();
					collegeName = college.getCollegeName();
				}
			}
			
			
			//更新
			service.updateCollege(collegeId, collegeName, id);
			
		}
	}

}
